package com.sgb.kjwl.viewmodel.demand;

import android.text.TextUtils;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.sgb.kjwl.model.entity.EnquiryTypeEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.demandHall.DemandHallListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DemandHallTabModel extends BaseViewModel {
    private DemandHallListFragment activity;
    private HomeRepertory repertory;
    private final int pageLimit = 10;
    public DemandHallTabModel(DemandHallListFragment activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }
//    获取状态类型
    public void getEnquiryType() {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getEnquiryType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    activity.getEnquirtTypeList(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });
    }
    //    获取用户类型
    public void getUserType() {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getUserType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    activity.getUserTypeList(response.body().getData());

                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });
    }

//    获取排序类型
    public void getSortType() {
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCommonAPI().getSortType().enqueue(new Callback<BaseEntity<List<EnquiryTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Response<BaseEntity<List<EnquiryTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                   activity.getSortTypeList(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<EnquiryTypeEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });
    }
//获取数据列表信息
    public void getDemandHallList(String userid, String sortid, String enquiryid, String fcatName,String searchname,int page ) {
        Map map =new HashMap();
        if (!TextUtils.isEmpty(userid)){
            map.put("authMethod",userid);
        }
        map.put("limit",pageLimit);
        map.put("page",page);
        map.put("sortType",sortid);
        if (!TextUtils.isEmpty(enquiryid)){
            map.put("status",enquiryid);
        }
        map.put("fcatName",fcatName+"");
        map.put("tcatName",searchname+"");
        DialogHelper.showProgressDialog(activity.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<ListResponse<List<DemandHallListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Response<BaseEntity<ListResponse<List<DemandHallListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                        activity.setDemandHallList(response.body().getData().getList());
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<DemandHallListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });
    }
//需求列表详情界面
public void showUrl(String no) {
    APIManager.getInstance().getLookingforAJobAPI().getUrlSetting("enquiryDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
        @Override
        public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
            if (response.body() == null || response.body().getState() == null) {
                return;
            }
            if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                if (!TextUtils.isEmpty(response.body().getData().getUrl())){
                    String url = response.body().getData().getUrl() + "?no=" + no;
                    WebViewX5HostActivity.start(activity.getActivity(), url);

                    //存储商品跳转h5标记
                    SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "event_shopping_list");
                }else {
                    WebViewX5HostActivity.start(activity.getActivity(), "");
                }
            } else {
                MToast.showToast(activity.getActivity(), response.body().getMsg());
            }
        }

        @Override
        public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
        }
    });
}
}
